package com.forhappy.light3ds8.server;


import java.util.HashMap;
import java.util.LinkedList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import android.util.Pair;

import com.forhappy.light3ds8.datatype.DataBasic;
import com.forhappy.light3ds8.db.LedDBMgr;
import com.forhappy.light3ds8.server.HttpUtils.Result;
import com.forhappy.light3ds8.shareprefrence.Config;
import com.forhappy.light3ds8.tools.Constant;

public class Job_SyncDataBasicInfo implements Runnable{
	private static final String TAG = "Job_SyncDataBasicInfo";
	Context mContext;

	public Job_SyncDataBasicInfo(Context context){
		mContext = context;
	}
	@Override
	public void run() {
		Log.d(TAG, "enter Job_SyncDataBasicInfo::run()");
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("opType", "getBasic");
		if(Config.getNoNeedSeeOthers(mContext)){
			params.put(Config.ITEM_NO_NEED_SEE_OTHERS, true);
		}

		Result res = HttpUtils.post(Constant.SERVER_DATA_URL, params);
		if(res.isSuccess()){
			Log.d(TAG, "call " + Constant.SERVER_DATA_URL + ", return success!!");
			Log.d(TAG, "ret: " + res);
			LedDBMgr dbMgr = new LedDBMgr(mContext);
			try {
				JSONArray jsArray = new JSONArray(res.msg);
				int len= jsArray.length();
				LinkedList<DataBasic> dataBasicList = new LinkedList<DataBasic>();
				for(int i = 0; i < len; i++){
					JSONObject jsObj = jsArray.getJSONObject(i);
					dataBasicList.add(new DataBasic(jsObj));
				}
				if(dataBasicList.size() > 0){
					dbMgr.reset();
					dbMgr.saveToDBBasic(dataBasicList);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		} else {
			Log.e(TAG, "call " + Constant.SERVER_DATA_URL + ", return failed!!");
		}
	}

}
